package com.wt.admin.controller;

import com.wt.admin.service.SmsCodeService;
import com.wt.admin.service.WqunAdminService;
import com.wt.commonutils.R;
import com.wt.commonutils.ResultMess;
import com.wt.servicebase.exceptionhandler.exceptionhandler.WQUNException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 阿里云短信 前端控制器
 * </p>
 *
 * @author wt
 * @since 2023-07-14
 */
@Api(tags = "短信模块")
@RestController
@RequestMapping("/loginservice/sms")
@CrossOrigin
public class SmsController {

    @Autowired
    private SmsCodeService smsCodeService;

    @Autowired
    private WqunAdminService accountService;
    // 注册验证码
    @ApiOperation("注册验证码")
    @PostMapping("/sendRegisterSms")
    public R smsRegister(@RequestBody String phone){

        ResultMess resultMess = new ResultMess();

        // 验证手机号正确性
        accountService.legalPhone(phone);

        //判断账号是否存在
        if(accountService.phoneYetRegister(phone)){
            throw new WQUNException(20003,resultMess.REGISTER_INFO_PHONE_IS_EXIST);
        }

        // 发送验证码
        if(smsCodeService.sentSms(phone)) return R.ok().message(resultMess.LOGIN_SUCCESS_PHONE_CODE_SEND);
        return R.error().message(resultMess.LOGIN_INFO_PHONE_CODE_SEND);
    }

    //登录验证码
    @ApiOperation("登录验证码")
    @GetMapping("/sendLoginSms/{phone}")
    public R smsLogin(@PathVariable("phone") String phone){
        ResultMess resultMess = new ResultMess();

        // 1、验证手机号正确性
        accountService.legalPhone(phone);

        //判断账号是否存在
        if(!accountService.phoneYetRegister(phone)){
            throw new WQUNException(20003,resultMess.LOGIN_INFO_PHONE_IS_NOT_EXIST);
        }

        // 发送验证码
        if(smsCodeService.sentSms(phone)) return R.ok().message(resultMess.LOGIN_SUCCESS_PHONE_CODE_SEND);
        return R.error().message(resultMess.LOGIN_INFO_PHONE_CODE_SEND);
    }


}
